// main.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick.Controls.Material 2.15
import QtQuick.Layouts 1.15
import QtGraphicalEffects 1.15
import "pages" 1.0

ApplicationWindow {
    id: mainWindow
    visible: false
    width: 1200
    height: 800
    minimumWidth: 900
    minimumHeight: 600
    title: "机器人智能管理平台"
    Material.theme: Material.Light       // 主题light
    Material.accent: Material.Indigo     // 靛蓝色
    Material.primary: Material.BlueGrey  // 蓝灰色

    Component.onCompleted: {
        // 延迟显示窗口，确保引擎初始化完成
        Qt.callLater(() => visible = true)
    }

    // 颜色常量定义
    QtObject {
        id: colors
        property var grey50: Material.color(Material.Grey, Material.Shade50)
        property var grey100: Material.color(Material.Grey, Material.Shade100)
    }

    // ====== 现代化的菜单栏 ======
    menuBar: MenuBar {
        id: appMenuBar
        Material.foreground: "white"
        Material.background: Material.primary

        Menu {
            title: "文件"
            Material.elevation: 2

            MenuItem {
                text: "退出"
                icon.source: "qrc:/icons/exit.svg"
                onTriggered: Qt.quit()
            }
        }

        Menu {
            title: "视图"

            MenuItem {
                text: "刷新"
                icon.source: "qrc:/icons/refresh.svg"
                onTriggered: {
                    console.log("手动刷新列表")
                    refreshAnimation.start()
                }

                SequentialAnimation {
                    id: refreshAnimation
                    loops: 1
                    PropertyAction { target: refreshIcon; property: "rotation"; value: 0 }
                    NumberAnimation {
                        target: refreshIcon
                        property: "rotation"
                        to: 360
                        duration: 800
                        easing.type: Easing.InOutQuad
                    }
                }

                Image {
                    id: refreshIcon
                    source: "qrc:/icons/refresh.svg"
                    visible: false
                }
            }
            
            MenuItem {
               text: "地图"
               icon.source: "qrc:/icons/map.svg"
               onTriggered: {
                   const component = Qt.createComponent("qrc:/qml/pages/MapViewerGlobal.qml");
                   if (component.status === Component.Ready) {
                       const window = component.createObject(mainWindow);
                       window.visible = true
                   } else {
                       console.error("地图组件加载失败:", component.errorString());
                   }
               }
           }
            
        }

        Menu {
            title: "帮助"

            MenuItem {
                text: "关于"
                icon.source: "qrc:/icons/info.svg"
                onTriggered: aboutDialog.open()
            }
        }
    }

    // ====== 现代化的页面容器 ======
    Rectangle {
        id: contentContainer
        anchors {
            top: parent.top 
            bottom: statusBar.top
            left: parent.left
            right: parent.right
        }

        gradient: Gradient {
            GradientStop { position: 0.0; color: colors.grey50 }
            GradientStop { position: 1.0; color: colors.grey100 }
        }

        Rectangle {
            id: contentCard
            anchors.fill: parent
            radius: 12
            color: Material.background
            layer.enabled: true
            layer.effect: DropShadow {
                radius: 16
                samples: 32
                color: "#40000000"
                verticalOffset: 4
            }

            StackView {
                id: stackView  // 恢复stackView
                anchors.fill: parent
                initialItem: ListViewPage {
                    stackView: stackView  // 向下传递引用
                }
            }
        }
    }

    // ====== 现代化的状态栏 ======
    ToolBar {
        id: statusBar
        anchors.bottom: parent.bottom
        width: parent.width
        Material.background: Material.primary
        Material.foreground: "white"

        Row {
            spacing: 20
            anchors.fill: parent
            leftPadding: 20

            Row {
                spacing: 8
                anchors.verticalCenter: parent.verticalCenter

                Rectangle {
                    width: 12
                    height: 12
                    radius: 6
                    color: "limegreen"
                    anchors.verticalCenter: parent.verticalCenter
                }

                Label {
                    text: "在线"
                    color: "white"
                    font.pixelSize: 12
                    anchors.verticalCenter: parent.verticalCenter
                }
            }

            Label {
                id: statusLabel
                text: "就绪"
                font.pixelSize: 14
                anchors.verticalCenter: parent.verticalCenter
            }

            Label {
                id: timeLabel
                text: Qt.formatDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")
                font.pixelSize: 12
                anchors.verticalCenter: parent.verticalCenter

                Timer {
                    id: timeTimer
                    interval: 1000
                    repeat: true
                    running: true
                    onTriggered: timeLabel.text = Qt.formatDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")
                }
            }
        }
    }

    // ====== 现代化的关于对话框 ======
    Popup {
        id: aboutDialog
        width: 400
        height: 300
        x: (parent.width - width) / 2
        y: (parent.height - height) / 2
        modal: true
        focus: true
        closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside

        background: Rectangle {
            radius: 12
            color: Material.background
            layer.enabled: true
            layer.effect: DropShadow {
                radius: 24
                samples: 48
                color: "#80000000"
            }
        }

        contentItem: Column {
            spacing: 20
            padding: 30

            Image {
                source: "qrc:/icons/app-icon.svg"
                anchors.horizontalCenter: parent.horizontalCenter
                sourceSize: Qt.size(80, 80)
            }

            Label {
                text: "机器人智能管理平台"
                font.bold: true
                font.pixelSize: 20
                anchors.horizontalCenter: parent.horizontalCenter
                color: Material.foreground
            }

            Label {
                text: "版本: v1.0.0"
                font.pixelSize: 16
                anchors.horizontalCenter: parent.horizontalCenter
                color: Material.color(Material.Grey)
            }

            Label {
                text: "© 2025 版权所有"
                font.pixelSize: 14
                anchors.horizontalCenter: parent.horizontalCenter
                color: Material.color(Material.Grey)
            }

            Button {
                text: "确定"
                anchors.horizontalCenter: parent.horizontalCenter
                Material.background: Material.accent
                Material.foreground: "white"
                onClicked: aboutDialog.close()
            }
        }
    }

    // 地图窗口引用
    property var mapWindow: null
}